package com.hsj.common.system.controller;

import com.cjxch.supermybatis.base.bean.Pager;
import com.hsj.common.system.convert.SysRoleMenuConvert;
import com.hsj.common.system.entity.SysRoleMenuDo;
import com.hsj.common.system.service.ISysRoleMenuService;
import com.hsj.common.system.vo.request.ReqSysRoleMenuCreateVo;
import com.hsj.common.system.vo.request.ReqSysRoleMenuUpdateVo;
import com.hsj.common.system.vo.request.ReqSysRoleMenuPageQueryVo;
import com.hsj.common.system.vo.response.RespSysRoleMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;


/**
 * 菜单角色关系 Controller
 * @Author: Czy
 * @Date: 2024-01-23 23:03:26
 * @Email: 314170122@qq.com
 */
@Api(tags = "菜单角色关系管理")
@RestController
@RequestMapping("/sysRoleMenu")
@Slf4j
public class SysRoleMenuController {

    private final ISysRoleMenuService sysRoleMenuService;
    private final SysRoleMenuConvert sysRoleMenuConvert;

    public SysRoleMenuController(ISysRoleMenuService sysRoleMenuService, SysRoleMenuConvert sysRoleMenuConvert) {
        this.sysRoleMenuService = sysRoleMenuService;
        this.sysRoleMenuConvert = sysRoleMenuConvert;
    }

    @PostMapping("createSysRoleMenu")
    @ApiOperation(value = "创建菜单角色关系", notes = "创建菜单角色关系", httpMethod = "POST")
    public Serializable createSysRoleMenu(@Valid @RequestBody ReqSysRoleMenuCreateVo vo){
        return sysRoleMenuService.insert(sysRoleMenuConvert.voToDo(vo));
    }

    @PostMapping("updateSysRoleMenu")
    @ApiOperation("修改菜单角色关系")
    public Serializable updateSysRoleMenu(@Valid @RequestBody ReqSysRoleMenuUpdateVo vo){
        return sysRoleMenuService.update(sysRoleMenuConvert.voToDo(vo));
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除菜单角色关系")
    public Boolean delete(@PathVariable("id")String id){
        return sysRoleMenuService.delete(id) == 1L;
    }

    @PostMapping("/pageList")
    @ApiOperation("分页查询菜单角色关系")
    public Pager<RespSysRoleMenuVo> pageList(@Valid @RequestBody ReqSysRoleMenuPageQueryVo vo){
        Pager<SysRoleMenuDo> pager = new Pager<>();
        pager.setPageNumber(vo.getPageNumber());
        pager.setPageSize(vo.getPageSize());
        pager = sysRoleMenuService.getPager(pager, vo.getSmCriteria());
        pager.setResult(sysRoleMenuConvert.doToVo(pager.getResult()));
        Pager<RespSysRoleMenuVo> retPager = new Pager<>();
        BeanUtils.copyProperties(pager, retPager);
        return retPager;
    }

    @GetMapping("/get/{id}")
    @ApiOperation("获取菜单角色关系信息")
    public RespSysRoleMenuVo get(@PathVariable("id")String id) {
        return sysRoleMenuConvert.doToVo(sysRoleMenuService.get(id));
    }
}
